<template>
  <div id="app">
    <MyHeader :title="'商品列表案例'"></MyHeader>
    <MyTable :list="list">
      <template v-slot:tou>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>

      <template v-slot:shen="scope">
        <td>{{ scope.item.id }}</td>
        <td>{{ scope.item.goods_name }}</td>
        <td>{{ scope.item.goods_price }}</td>
        <td>
          <input
          @keydown.enter="add(scope.item)"
          @keydown.esc="scope.item.inputValue=''"
          v-model="scope.item.inputValue"
            v-focus
            @blur="scope.item.inputVisible = false"
            v-if="scope.item.inputVisible"
            type="text"
          />
          <button
        
            @click="scope.item.inputVisible = true"
              v-else
            class="btn btn-primary btn-sm"
          >
            +Tag</button
          ><br />

          <span
            class="badge bg-warning text-dark op"
            v-for="(item, index) in scope.item.tags"
            :key="index"
          >
            {{ item }}
          </span>
        </td>
        <td>
          <button @click="odd(scope.item.id)" class="btn btn-danger btn-sm">
            删除
          </button>
        </td>
      </template>
    </MyTable>
  </div>
</template>

<script>
import MyHeader from "../components/MyHeader.vue";
import MyTable from "../components/MyTable.vue";
export default {
  name: "ProductlistMygoodslist",
  components: {
    MyHeader,
    MyTable,
  },
  data() {
    return {
      list: [],
    };
  },

  created() {
    this.$axios({
      url: "/api/goods",
    }).then(({ data: res }) => {
      console.log(res.data);
      this.list = res.data;
    });
  },
  mounted() {
    console.log(this.list);
  },

  methods: {
    odd(val) {
      this.list.splice(
        this.list.findIndex((item) => item.id == val),
        1
      );
    },
    add(val){
   if(val.inputValue==''){
     return alert('不能输入空')
   }else{
     val.tags.push(val.inputValue)
     val.inputValue=''
   }
    
    }
  },
};
</script>

<style lang="less" scoped>
#app {
  padding-top: 50px;
  .op {
    margin-right: 5px;
  }
}
</style>
